#include "GameGlobal.h"

#include "GameBGLayer.h"

#include "TitleScene.h"
#include "HelloWorldScene.h"

#include "ControlLayer.h"

#include "SpriteBrick.h"

// on "init" you need to initialize your instance
bool GameBGLayer::init()
{
	bool bRet = false;
	do 
	{
		GameSetting::isGameStart = false;

		CC_BREAK_IF(! CCLayer::init());

		CCSize size = CCDirector::sharedDirector()->getWinSize();

		// background
		CCSprite* bgSprite = CCSprite::create("images/interface/background1.jpg");
		bgSprite->setPosition(VisibleRect::center());
		bgSprite->setAnchorPoint(RectLayout::Center());
		this->addChild(bgSprite,kBackgroundSprite,kBackgroundSprite);

		// zombies
		CCSprite* zombie0 = CCSprite::create("images/Zombies/ZombieEdit/Zombie0.png");
		CC_BREAK_IF(! zombie0);
		CCPoint right = VisibleRect::right();
		zombie0->setPosition(ccp(right.x + 0,right.y));
		zombie0->setAnchorPoint(RectLayout::Center());
		this->addChild(zombie0);

		// start scene
		this->schedule(schedule_selector(GameBGLayer::startScene),1.0);

		bRet = true;

	} while (0);

	return bRet;
}

void GameBGLayer::startScene(float dt)
{
	this->unschedule(schedule_selector(GameBGLayer::startScene));

	CCSprite* pbg = (CCSprite*)this->getChildByTag(kBackgroundSprite);

	// move layer
	float bgWidth  = pbg->getTextureRect().origin.x;
	float bgHeight = pbg->getTextureRect().origin.y;

	float moveOffsetX = (bgWidth - VisibleRect().center().x) /2;

	CCMoveBy* moveRight = CCMoveBy::create(2, ccp(moveOffsetX,0));
	CCDelayTime* delayTime = CCDelayTime::create(2);
	CCMoveBy* moveLeft = CCMoveBy::create(1, VisibleRect::bottom());
	CCMoveBy* moveRight2 = CCMoveBy::create(1, ccp(-100,0));
	CCSequence* acitons = CCSequence::create(moveRight,delayTime,moveLeft,moveRight2,NULL);
	this->runAction(acitons);

	this->schedule(schedule_selector(GameBGLayer::startTitleAnimation),8.0);
}

void GameBGLayer::startTitleAnimation(float dt)
{
	this->unschedule(schedule_selector(GameBGLayer::startTitleAnimation));

	// start title
	CCSpriteFrame *frame0, *frame1, *frame2;

	frame0 = CCSpriteFrame::create("images/PrepareGrowPlants0.png",CCRect::CCRect(0,0,256,256));
	frame1 = CCSpriteFrame::create("images/PrepareGrowPlants1.png",CCRect::CCRect(0,0,256,256));
	frame2 = CCSpriteFrame::create("images/PrepareGrowPlants2.png",CCRect::CCRect(0,0,256,256));

	CCSprite* startTitle = CCSprite::createWithSpriteFrame(frame0);
	startTitle->setPosition(VisibleRect::center());
	startTitle->setAnchorPoint(RectLayout::Center());

	this->addChild(startTitle,kStartTitleSprite,kStartTitleSprite);

	CCArray* animFrames = CCArray::createWithCapacity(3);
	animFrames->addObject(frame0);
	animFrames->addObject(frame1);
	animFrames->addObject(frame2);
	CCAnimation *animation = CCAnimation::createWithSpriteFrames(animFrames, 1.0f);
	CCRepeat* actionStartTitleMove = CCRepeat::create(CCAnimate::create(animation),1);
	CCFiniteTimeAction* actionStartTitleDone = CCCallFuncN::create( this, callfuncN_selector(GameBGLayer::spriteStartTitleMoveFinished));

	startTitle->runAction( CCSequence::create(actionStartTitleMove,actionStartTitleDone, NULL));
}

void GameBGLayer::spriteStartTitleMoveFinished(CCNode* sender)
{
	CCSprite *sprite = (CCSprite *)sender;
	this->removeChild(sprite, true);
	GameSetting::isGameStart = true;
}